package com.xiaoxi.exam.service;

import com.xiaoxi.exam.bean.ToeflProvinceCity;
import com.xiaoxi.exam.dao.ToeflSchoolMapper;
import com.xiaoxi.exam.model.ToeflSchool;
import com.xiaoxi.exam.model.ToeflSchoolExample;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ToeflSchoolService {

  @Autowired
  private ToeflSchoolMapper toeflSchoolMapper;

  public List<ToeflProvinceCity> getCities() {
    ToeflSchoolExample example = new ToeflSchoolExample();
    example.or().andDeletedAtIsNull();
    List<ToeflSchool> toeflSchools = toeflSchoolMapper.selectByExample(example);
    return toeflSchools.stream().collect(Collectors.groupingBy(ToeflSchool::getProvince)).entrySet()
        .stream().map(e -> {
          ToeflProvinceCity tpc = new ToeflProvinceCity();
          tpc.setProvince(e.getKey());
          tpc.setCities(e.getValue().stream().map(ToeflSchool::getCity).distinct()
              .collect(Collectors.toList()));
          return tpc;
        }).collect(Collectors.toList());
  }

  public List<String> getSchools(String province, String city) {
    ToeflSchoolExample example = new ToeflSchoolExample();
    example.or().andProvinceEqualTo(province).andCityEqualTo(city).andDeletedAtIsNull();
    return toeflSchoolMapper.selectByExample(example).stream().map(ToeflSchool::getSchool)
        .distinct().collect(Collectors.toList());
  }

}
